// Town script for town 6: Church

begintownscript;

variables;
int choice;
short tick1,tick2;
int zombie,number;

body;

beginstate INIT_STATE;
set_flag(250,0,10); // set flag correctly for zombie invasion

if (get_flag(6,12) == 0)
		set_flag(6,12,1); // this flag denotes that you have been in town 6 and that the resting sequence can begin
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
if (get_flag(6,9) == 0 && get_flag(6,6) == 1) { // door bashing sounds when zombies are through barricades but not yet in church
		number = get_ran(1,1,7);
		if (number == 1)
				play_sound(5);
		if (number == 2) {
				play_sound(5);
				play_sound(5);
				}
		if (number == 3) {
				play_sound(5);
				play_sound(5);
				play_sound(5);
				}
		}
		

if (get_flag(6,0)==0) {
	message_dialog("After a very short walk, we reach the church grounds. Luckily, something has breached the gate here, so we can enter.","The church seems to be indeed quite a beautiful building, and what's more, it has hardly been damaged. We should go inside, quickly!");
	set_flag(6,0,1);
	}

// Check tick1
tick1 = get_flag(6,2) * 256 + get_flag(6,3);
if (tick1 + 30 == get_current_tick() && get_flag(6,1) == 1&& (get_terrain(32,24) != 11 || get_terrain(32,23) != 11 || get_terrain(32,22) != 11 || get_terrain(20,33) != 10 || get_terrain(21,33) != 10 || get_terrain(22,33) != 10 || get_terrain(20,13) != 12 || get_terrain(21,13) != 12 || get_terrain(22,13) != 12))
		message_dialog("Come on man, we should close all the doors! What are we waiting for?!","");


// check if all doors closed, then show message
if (get_flag(6,5) == 0 && get_terrain(32,24) == 11 && get_terrain(32,23) == 11 && get_terrain(32,22) == 11 && get_terrain(20,33) == 10 && get_terrain(21,33) == 10 && get_terrain(22,33) == 10 && get_terrain(20,13) == 12 && get_terrain(21,13) == 12 && get_terrain(22,13) == 12) {
		message_dialog("Now that all doors are closed, let's go sit at the altar, the holiest place in this church. Then we'll try and think of a way out of here.","");
		set_flag(6,11,1);
		set_flag(6,5,1);
		}

// noises of zombies
if (get_flag(6,6) == 1) { // if zombies are coming, run sounds
		if (get_ran(1,1,8) == 8)
				play_sound(44);

		if (get_ran(1,1,8) == 8)
				play_sound(119);
		}

// zombie controller
if (get_flag(250,0) < 10) {
		zombie = get_ran(1,240,242);
		place_monster(42,23,zombie,0);
		inc_flag(250,0,1);
		}
break;

beginstate 10;
message_dialog("Nah, we don't want to go back to the center of Pilgrim's Rest. We're trying to get away from the zombies, not go to their source.","");
block_entry(1);
break;

beginstate 11;
if (is_combat() == 0)
		move_to_new_town(7,25,39);

message_dialog("We can only go on when we have regrouped and are not fighting, so let's do that first.","");
block_entry(1);
break;

beginstate 12;
if (get_flag(6,1)==0 && get_flag(6,5) != 1) {
	message_dialog("Good, we are in. Now, close the doors, quickly! You never know, maybe the zombies will come here eventually, and then we don't want them to walk right in.","");
	set_flag(6,1,1);
	tick1 = get_current_tick();
	set_flag(6,2,tick1 / 256);
	set_flag(6,3,tick1 % 256);
	}
break;

beginstate 13;
if(get_flag(6,4)==0){
	message_dialog("This is the way to the cemetery, but the gate appears to be locked and it won't budge for anything. If we are gonna go there, we'll have to find another route.","");
	set_flag(6,4,1);
}
break;

beginstate 14;
if (get_flag(6,6) == 0 && get_flag(6,5) == 1) {
		reset_dialog();
		add_dialog_str(0,"Now let's calm down a bit from all the running around. This church is quite nice. Not very big, but it does the job. Pretty altar. Everything's a bit dusty and the cobwebs are starting to dominate the walls, but what can you do.",0);
		add_dialog_str(1,"Still a wonder that everything seems to be in a relatively good state after all this time. Compared to Pilgrim's Rest, this old church is a monument of peace and cleanliness.",0);
		add_dialog_str(2,"We can only guess there simply was no fighting in and around this church, although the vegetation is close to being dead. The trees look awful, and the grass is this ugly, pale green.",0);
		add_dialog_str(3,"Could the disease that affected the townspeople be affecting the foliage? If that's so, that's a very bad sign. Affecting foliage, it could hop from plant to plant, effectively killing this entire region.",0);
		add_dialog_choice(0,"Okay");
		choice = run_dialog(1);

		reset_dialog();
		add_dialog_str(0,"Now, to recap what happened:",0);
		add_dialog_str(1,"The woods are empty, no beast lurking in a radius of ten miles around town for all we know. Then, Pilgrim's Rest. Inhabitants either killed by a disease or by their fellow townspeople.",0);
		add_dialog_str(2,"Quite a way to die, turning into a zombie... Would it hurt? All these people must have been feeling that their end is near. What a grave feeling it must be. Knowing that you are dying, but not knowing how to stop it.",0);
		add_dialog_str(3,"It's evident: we need to get out of here. If we are too slow, either the zombis or the disease will get to us. We'll follow the same path as the inhabitants of Pilgrim's rest did and we'll belong to the group we are now trying to avoid so frantically.",0);
		add_dialog_choice(0,"");
		choice = run_dialog(1);

		reset_dialog();
		add_dialog_str(0,"So let's sit and think. We need to get out of here, but where to? Going back to Pilgrim's Rest is right out. The only direction we can go would be the cemetery and hope we can get out somehow through there.",0);
		add_dialog_str(1,"It's certainly not great. I mean, chased by zombies, to a place that should be full of them? Anything's better than that...",0);
		add_dialog_choice(0,"");
		choice = run_dialog(1);

		play_sound(108);
		pause(10);

		play_sound(108);
		play_sound(108);
		pause(10);

		message_dialog("That must be the zombies already breaking through the barricades in front of the church! We have to run from this gloomy deathtrap.","To the cemetery, quickly!");

		activate_hidden_group(1);
		set_flag(6,6,1);

		tick2 = get_current_tick();
		set_flag(6,7,tick2 / 256);
		set_flag(6,8,tick2 % 256);
		set_flag(6,11,2);
		}
break;

beginstate 15;
if (get_flag(6,9) == 0 && get_flag(6,6) == 1) {
		message_dialog("We must stay nearby the altar. It may be the only place safe in this godforsaken church.","");
		block_entry(1);
		}
break;

beginstate 16;
if (get_terrain(32,24) != 11 || get_terrain(32,23) != 11 || get_terrain(32,22) != 11 || get_terrain(20,33) != 10 || get_terrain(21,33) != 10 || get_terrain(22,33) != 10 || get_terrain(20,13) != 12 || get_terrain(21,13) != 12 || get_terrain(22,13) != 12) { // if doors not closed, block entry
		message_dialog("Before we start going places, let's just first close all the doors, shall we?","");
		block_entry(1);
		end();
		}

if (get_flag(6,11) == 1) {
		message_dialog("We might go there later. Let's first just sit next to the altar to see how we are going to escape this place.",""); // if not sat at altar yet, block entry
		block_entry(1);
		}
break;

beginstate 17; // zombies invading church
if (get_flag(6,9) == 0) {
		// place zombies
		relocate_character(6,32,22);
		relocate_character(7,20,13);
		relocate_character(8,20,33);
		relocate_character(9,32,23);
		relocate_character(10,21,13);
		relocate_character(11,21,33);
		relocate_character(12,32,24);
		relocate_character(13,22,13);
		relocate_character(14,22,33);
		relocate_character(15,33,23);

		set_total_visibility(1);
		force_instant_terrain_redraw();

		force_view_center(21,31);
		force_instant_terrain_redraw();
		// Crush the doors
		pause(10);
		set_terrain(20,33,137);
		force_instant_terrain_redraw();
		play_sound(108);
		pause(5);

		set_terrain(21,33,137);
		force_instant_terrain_redraw();
		play_sound(108);
		pause(5);

		set_terrain(22,33,137);
		force_instant_terrain_redraw();
		play_sound(108);
		pause(15);

		force_view_center(21,15);
		force_instant_terrain_redraw();
		pause(10);
		set_terrain(20,13,137);
		force_instant_terrain_redraw();
		play_sound(108);
		pause(5);

		set_terrain(21,13,137);
		force_instant_terrain_redraw();
		play_sound(108);
		pause(5);

		set_terrain(22,13,137);
		force_instant_terrain_redraw();
		play_sound(108);
		pause(15);

		force_view_center(30,23);
		force_instant_terrain_redraw();
		pause(10);
		set_terrain(32,22,137);
		force_instant_terrain_redraw();
		play_sound(108);
		pause(5);

		set_terrain(32,23,137);
		force_instant_terrain_redraw();
		play_sound(108);
		pause(5);

		set_terrain(32,24,137);
		force_instant_terrain_redraw();
		play_sound(108);
		pause(15);

		force_view_center(char_loc_x(0),char_loc_y(0));
		force_instant_terrain_redraw();
		pause(10);

		set_total_visibility(0);
		force_instant_terrain_redraw();

		if (get_flag(6,10) == 1) {
				message_dialog("Damn, the zombies have already come through the church doors. We must make haste!","");
				}

		if (get_flag(6,10) == 0)
				message_dialog("The zombies are in! Quickly, we must find a way out through the back, or we can shake it.","");

		set_flag(6,9,1);
		}
break;